package cn.bocweb.spinning.view.forun;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.ForumList2Bean;
import cn.bocweb.spinning.model.Bean.ForumListBean;
import cn.bocweb.spinning.presenter.Imp.UserPresenterImp;
import cn.bocweb.spinning.presenter.UserPresenter;
import cn.bocweb.spinning.util.Constant;
import cn.bocweb.spinning.util.DeviceUtil;
import cn.bocweb.spinning.view.adapter.BannerAdapter;
import cn.bocweb.spinning.view.common.SwipeRefreshHelper;
import de.greenrobot.event.EventBus;

/**
 * Created by chenchangxing on 16/3/11.
 */
public class ForunSpecialFragment extends Fragment {

    int type,// 1为热度,2为精品
            page = 1;
    boolean isLastPage;
    boolean hasLoad;
    UserPresenter presenter;
    ForunSpecialAdapter forunSpecialAdapter;
    ConvenientBanner forunSpecialBanner;
    List<String> baseUri, bannerTitles;
    TextView bannerTitle;
    List<ForumListBean.DataBean> forumList;
    Timer mTimer;
    SwipeRefreshHelper mSwipeRefreshHelper;
    @Bind(R.id.forun_special_list)
    ListView forunSpecialList;
    @Bind(R.id.forun_refresh)
    SwipeRefreshLayout mForunRefresh;


    public ForunSpecialFragment(int type) {
        this.type = type;

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_forun_special, container, false);
        mTimer = new Timer();
        EventBus.getDefault().register(this);
        ButterKnife.bind(this, view);
        return view;
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        forumList = new ArrayList<>();
        forunSpecialAdapter = new ForunSpecialAdapter(getActivity(), forumList);
        baseUri = new ArrayList<>();
        bannerTitles = new ArrayList<>();

        presenter = new UserPresenterImp();

        initListView();
    }

    @Override
    public void onResume() {
        super.onResume();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        if (!forumList.isEmpty()) {
            forumList.clear();
            forunSpecialAdapter.notifyDataSetChanged();
        }
        hasLoad = false;
        page = 1;
        if (type == 1) {
            presenter.getForum1List(page, type);
        } else {
            presenter.getForum2List(page, type);
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
        forunSpecialBanner.stopTurning();
    }

    /**
     * 网络回调
     */

    public void onEventMainThread(ForumListBean bean) {
        if (mForunRefresh.isRefreshing()) {
            mForunRefresh.setRefreshing(false);
        }
        if (bean.getReturn_code() == 0) {
            if (type == 1) {
                for (ForumListBean.SliderBean img : bean.getSlider()) {
                    baseUri.add(img.getPhoto());
                    bannerTitles.add(img.getTitle());
                }
                forunSpecialBanner.notifyDataSetChanged();
                forunSpecialBanner.startTurning(3000);
                if (bean.getData() != null) {
                    forumList.addAll(bean.getData());
                    forunSpecialAdapter.notifyDataSetChanged();
                }
            }
        } else {
            if (type == 1) {
                if (bean.getReturn_code() == -203) {
                    for (ForumListBean.SliderBean img : bean.getSlider()) {
                        baseUri.add(img.getPhoto());
                        bannerTitles.add(img.getTitle());
                    }
                    forunSpecialBanner.notifyDataSetChanged();
                    forunSpecialBanner.startTurning(3000);
                } else if (bean.getReturn_code() == -202){
                    isLastPage = true;
                } else {
                    DeviceUtil.MyToast(getActivity(), bean.getMsg());
                }
            }
        }
    }

    public void onEventMainThread(ForumList2Bean bean) {
        if (mForunRefresh.isRefreshing()) {
            mForunRefresh.setRefreshing(false);
        }
        if (bean.getReturn_code() == 0) {
            if (type == 2) {
                for (ForumList2Bean.SliderBean img : bean.getSlider()) {
                    baseUri.add(img.getPhoto());
                    bannerTitles.add(img.getTitle());
                }
                forunSpecialBanner.notifyDataSetChanged();
                forunSpecialBanner.startTurning(3000);
                if (bean.getData() != null) {
                    List<ForumListBean.DataBean> dataBeanList = new ArrayList<>();
                    for (ForumList2Bean.DataBean bean1 : bean.getData()) {
                        ForumListBean.DataBean m = new ForumListBean.DataBean();
                        m.setContent(bean1.getContent());
                        m.setCount(bean1.getCount());
                        m.setId(bean1.getId());
                        m.setNickname(bean1.getNickname());
                        m.setPhoto(bean1.getPhoto());
                        m.setTimeline(bean1.getTimeline());
                        m.setUpnum(bean1.getUpnum());
                        m.setTitle(bean1.getTitle());
                        m.setUthumb(bean1.getUthumb());
                        dataBeanList.add(m);
                    }
                    forumList.addAll(dataBeanList);
                    forunSpecialAdapter.notifyDataSetChanged();
                }
            }
        } else {
            if (type == 2) {
                if (bean.getReturn_code() == -203) {
                    for (ForumList2Bean.SliderBean img : bean.getSlider()) {
                        baseUri.add(img.getPhoto());
                        bannerTitles.add(img.getTitle());
                    }
                    forunSpecialBanner.notifyDataSetChanged();
                    forunSpecialBanner.startTurning(3000);
                } else if (bean.getReturn_code() == -202){
                    isLastPage = true;
                } else {
                    DeviceUtil.MyToast(getActivity(), bean.getMsg());
                }
            }
        }
    }

    private void initListView() {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.view_info_banner, null);
        forunSpecialBanner = (ConvenientBanner) view.findViewById(R.id.info_banner);
        bannerTitle = (TextView) view.findViewById(R.id.info_banner_title);

        forunSpecialBanner.setPages(new CBViewHolderCreator<BannerAdapter>() {
            @Override
            public BannerAdapter createHolder() {
                return new BannerAdapter();
            }
        }, baseUri);

        forunSpecialBanner.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                bannerTitle.setText(bannerTitles.get(position));
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        forunSpecialList.addHeaderView(view);
        forunSpecialList.setAdapter(forunSpecialAdapter);

        mSwipeRefreshHelper = new SwipeRefreshHelper(mForunRefresh, forunSpecialList, new SwipeRefreshHelper.OnSwipeRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                forumList.clear();
                hasLoad = false;
                isLastPage = false;
                forunSpecialAdapter.notifyDataSetChanged();
                if (type == 1) {
                    presenter.getForum1List(page, type);
                } else {
                    presenter.getForum2List(page, type);
                }
            }

            @Override
            public void onLoad() {
                if (!isLastPage) {
                    page++;
                    if (type == 1) {
                        presenter.getForum1List(page, type);
                    } else {
                        presenter.getForum2List(page, type);
                    }
                } else {

                    if (!hasLoad) {
                        DeviceUtil.MyToast(getActivity(), "已经是最后一页");
                        hasLoad = true;
                    }
                }
            }
        });

        forunSpecialList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), ForunDetailActivity.class);
                intent.putExtra(Constant.TO_FORUM_DETAIL, forumList.get(position - 1).getId());
                startActivity(intent);
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
